<?php

namespace App\Http\Controllers\Member;

use App\Http\Controllers\Controller;
use App\Services\UploadService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Spatie\Image\Image;
use Spatie\Image\Manipulations;

class AvatarController extends Controller
{
    //
    public function create()
    {
        return view('member.avatar');
    }

    public function store(Request $request)
    {
        $user = Auth::user();
        $user->avatar = $request->avatar;
        $user->save();
        return back()->with('success', '头像设置成功');
    }

    public function upload(Request $request, UploadService $uploadService)
    {
        //上传的后台要实现验证，前台验证引导优化用户体验，后端严格验证上传
        $request->validate([
            'avatar' => ['mimes:jpeg,bmp,png']
        ]);
        $path=$request->file('file')->Path();
        Image::load($path)
            ->crop(Manipulations::CROP_TOP_RIGHT,250,250)
            ->save();
        //上传文件
        return $uploadService->upload($request->file);
    }
}
